<?php
//引入数据库配置文件
$config=require(dirname(__FILE__).'/config.php');
class Connection{
     //主机
     private $host;
     //数据库的username
     private $user;
     //数据库的password
     private $password;
     //数据库名称
     private $dbname;
     //编码形式
     private $charset="utf8"; 
     //数据库链接
     private $conn; 

     //构造函数
     function __construct(){
		global $config;		//声明全局变量
        date_default_timezone_set('PRC');
        $this->host=$config['DB_HOST']; 
		$this->dbname=$config['DB_NAME'];
        $this->user=$config['DB_USER'];
        $this->password=$config['DB_PWD'];
        self::link();
     }
     //数据库的链接
     function link(){
         $this->conn=mysqli_connect($this->host,$this->user,$this->password,$this->dbname) or die ('数据库链接失败:'.$this->error());
        //  mysqli_select_db($this->dbname,$link) or die("没该数据库：".$this->dbname);
         mysqli_query($this->conn,"SET NAMES '$this->charset'");
     }
     //执行sql语句
     function query($sql, $type = '') {
         if(!($query = mysqli_query($this->conn,$sql))) die($sql);
		 return $query;
     }
 	 //以索引数组的形式, 返回一条查询结果
	 function get_one($query) {
         return mysqli_fetch_assoc($query);
     }
	 //以索引数组的形式, 返回所有查询结果
	 function fetch_assoc($query) {
		while($row=mysqli_fetch_assoc($query)) {
			$list[]=$row;
		}
        return $list;
     }
}
?>